import 'json_convert_content.dart';

class BaseResponse<T> {
  int code;
  String message;
  T data;

  bool isSuccess() => code == 200;

  BaseResponse({this.code, this.message, this.data});

  BaseResponse.fromJson(Map<String, dynamic> map) {
    code = map['code'] as int;
    message = map['message'] as String;
    if (map.containsKey('data') && isSuccess()) {
      data = _generateOBJ<T>(map['data']);
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['code'] = this.code;
    data['data'] = this.data;
    data['message'] = this.message;
    return data;
  }

  T _generateOBJ<T>(dynamic json) {
    if (json == null) {
      return null;
    } else if (json is T) {
      return json;
    } else {
      return JsonConvert.fromJsonAsT<T>(json);
    }
  }
}
